<template>
  <el-dialog
    :title="!dataForm.docterId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="医师名称" prop="docterName">
      <el-input v-model="dataForm.docterName" placeholder="医师名称"></el-input>
    </el-form-item>
    <el-form-item label="医院id" prop="hospitalId">
      <el-input v-model="dataForm.hospitalId" placeholder="医院id"></el-input>
    </el-form-item>
    <el-form-item label="科室id" prop="departmentId">
      <el-input v-model="dataForm.departmentId" placeholder="科室id"></el-input>
    </el-form-item>
    <el-form-item label="等级名称" prop="gradeName">
      <el-input v-model="dataForm.gradeName" placeholder="等级名称"></el-input>
    </el-form-item>
    <el-form-item label="年限" prop="docterAge">
      <el-input v-model="dataForm.docterAge" placeholder="年限"></el-input>
    </el-form-item>
    <el-form-item label="医师资格证书" prop="docterCertification">
      <el-input v-model="dataForm.docterCertification" placeholder="医师资格证书"></el-input>
    </el-form-item>
    <el-form-item label="医师执业证书" prop="docterPractice">
      <el-input v-model="dataForm.docterPractice" placeholder="医师执业证书"></el-input>
    </el-form-item>
    <el-form-item label="身份证正面" prop="docterCardid">
      <el-input v-model="dataForm.docterCardid" placeholder="身份证正面"></el-input>
    </el-form-item>
    <el-form-item label="审核" prop="docterAudit">
      <el-input v-model="dataForm.docterAudit" placeholder="审核"></el-input>
    </el-form-item>
    <el-form-item label="是否开启问诊" prop="docterAuditisInquiry">
      <el-input v-model="dataForm.docterAuditisInquiry" placeholder="是否开启问诊"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          docterId: 0,
          docterName: '',
          hospitalId: '',
          departmentId: '',
          gradeName: '',
          docterAge: '',
          docterCertification: '',
          docterPractice: '',
          docterCardid: '',
          docterAudit: '',
          docterAuditisInquiry: ''
        },
        dataRule: {
          docterName: [
            { required: true, message: '医师名称不能为空', trigger: 'blur' }
          ],
          hospitalId: [
            { required: true, message: '医院id不能为空', trigger: 'blur' }
          ],
          departmentId: [
            { required: true, message: '科室id不能为空', trigger: 'blur' }
          ],
          gradeName: [
            { required: true, message: '等级名称不能为空', trigger: 'blur' }
          ],
          docterAge: [
            { required: true, message: '年限不能为空', trigger: 'blur' }
          ],
          docterCertification: [
            { required: true, message: '医师资格证书不能为空', trigger: 'blur' }
          ],
          docterPractice: [
            { required: true, message: '医师执业证书不能为空', trigger: 'blur' }
          ],
          docterCardid: [
            { required: true, message: '身份证正面不能为空', trigger: 'blur' }
          ],
          docterAudit: [
            { required: true, message: '审核不能为空', trigger: 'blur' }
          ],
          docterAuditisInquiry: [
            { required: true, message: '是否开启问诊不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.docterId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.docterId) {
            this.$http({
              url: this.$http.adornUrl(`/medical/tbdockerlevel/info/${this.dataForm.docterId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.docterName = data.tbDockerLevel.docterName
                this.dataForm.hospitalId = data.tbDockerLevel.hospitalId
                this.dataForm.departmentId = data.tbDockerLevel.departmentId
                this.dataForm.gradeName = data.tbDockerLevel.gradeName
                this.dataForm.docterAge = data.tbDockerLevel.docterAge
                this.dataForm.docterCertification = data.tbDockerLevel.docterCertification
                this.dataForm.docterPractice = data.tbDockerLevel.docterPractice
                this.dataForm.docterCardid = data.tbDockerLevel.docterCardid
                this.dataForm.docterAudit = data.tbDockerLevel.docterAudit
                this.dataForm.docterAuditisInquiry = data.tbDockerLevel.docterAuditisInquiry
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/medical/tbdockerlevel/${!this.dataForm.docterId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'docterId': this.dataForm.docterId || undefined,
                'docterName': this.dataForm.docterName,
                'hospitalId': this.dataForm.hospitalId,
                'departmentId': this.dataForm.departmentId,
                'gradeName': this.dataForm.gradeName,
                'docterAge': this.dataForm.docterAge,
                'docterCertification': this.dataForm.docterCertification,
                'docterPractice': this.dataForm.docterPractice,
                'docterCardid': this.dataForm.docterCardid,
                'docterAudit': this.dataForm.docterAudit,
                'docterAuditisInquiry': this.dataForm.docterAuditisInquiry
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
